import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
    history: createWebHistory(),
    routes: [
        {
            path: '/',
            name: 'login',
            component: () => import('../views/LoginView.vue')
        },
        {
            path: '/dashboard',
            name: 'dashboard',
            component: () => import('../views/DashboardView.vue'),
            meta: { requiresAuth: true, title: '系统首页' }
        },
        {
            path: '/personal-center',
            name: 'personal-center',
            component: () => import('../views/PersonalCenter.vue'),
            meta: { requiresAuth: true, title: '个人中心' }
        },
        // ========== 新增路由 ==========
        {
            path: '/product-management',
            name: 'product-management',
            component: () => import('../views/ProductManagement.vue'),
            meta: { requiresAuth: true, title: '商品管理' }
        },
        {
            path: '/user-management',
            name: 'user-management',
            component: () => import('../views/UserManagement.vue'),
            meta: { requiresAuth: true, title: '用户管理' }
        },
        {
            path: '/order-management',
            name: 'order-management',
            component: () => import('../views/OrderManagement.vue'),
            meta: { requiresAuth: true, title: '订单管理' }
        }
    ]
})

// 路由守卫保持不变
router.beforeEach((to, from, next) => {
    const token = localStorage.getItem('token')

    if (to.meta.requiresAuth && !token) {
        next('/')
    } else {
        // 设置页面标题
        if (to.meta.title) {
            document.title = `${to.meta.title} - EasyGo`
        }
        next()
    }
})

export default router